{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "7c61bd17-13ea-4019-905a-4b5bba731fe9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import statsmodels.api as sm\n",
    "import matplotlib. pyplot as plt\n",
    "from statsmodels.regression.rolling import RollingOLS\n",
    "from pandas.core.frame import DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e4dafdb5-4db5-4678-9a36-84b6e641211d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MarkettypeID</th>\n",
       "      <th>TradingDate</th>\n",
       "      <th>RiskPremium1</th>\n",
       "      <th>RiskPremium2</th>\n",
       "      <th>SMB1</th>\n",
       "      <th>SMB2</th>\n",
       "      <th>HML1</th>\n",
       "      <th>HML2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>P9706</td>\n",
       "      <td>1990-12-19</td>\n",
       "      <td>2.473374</td>\n",
       "      <td>2.309671</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>P9710</td>\n",
       "      <td>1990-12-19</td>\n",
       "      <td>2.473374</td>\n",
       "      <td>2.309671</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>P9709</td>\n",
       "      <td>1990-12-19</td>\n",
       "      <td>2.473374</td>\n",
       "      <td>2.309671</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>P9712</td>\n",
       "      <td>1990-12-19</td>\n",
       "      <td>2.473374</td>\n",
       "      <td>2.309671</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>P9713</td>\n",
       "      <td>1990-12-19</td>\n",
       "      <td>2.473374</td>\n",
       "      <td>2.309671</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  MarkettypeID TradingDate  RiskPremium1  RiskPremium2  SMB1  SMB2  HML1  HML2\n",
       "0        P9706  1990-12-19      2.473374      2.309671   NaN   NaN   NaN   NaN\n",
       "1        P9710  1990-12-19      2.473374      2.309671   NaN   NaN   NaN   NaN\n",
       "2        P9709  1990-12-19      2.473374      2.309671   NaN   NaN   NaN   NaN\n",
       "3        P9712  1990-12-19      2.473374      2.309671   NaN   NaN   NaN   NaN\n",
       "4        P9713  1990-12-19      2.473374      2.309671   NaN   NaN   NaN   NaN"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df=pd.read_csv('STK_MKT_THRFACDAY.csv',encoding='utf_8_sig')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "513a4d1d-c134-4bd1-a3e5-40e89810e438",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\anaconda\\lib\\site-packages\\pandas\\core\\indexing.py:1773: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  self._setitem_single_column(ilocs[0], value, pi)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MarkettypeID</th>\n",
       "      <th>RiskPremium1</th>\n",
       "      <th>RiskPremium2</th>\n",
       "      <th>SMB1</th>\n",
       "      <th>SMB2</th>\n",
       "      <th>HML1</th>\n",
       "      <th>HML2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-01-04</th>\n",
       "      <td>P9706</td>\n",
       "      <td>0.010357</td>\n",
       "      <td>0.010930</td>\n",
       "      <td>0.004638</td>\n",
       "      <td>0.004161</td>\n",
       "      <td>-0.010758</td>\n",
       "      <td>-0.011928</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-05</th>\n",
       "      <td>P9706</td>\n",
       "      <td>0.010340</td>\n",
       "      <td>0.010769</td>\n",
       "      <td>-0.012563</td>\n",
       "      <td>-0.011595</td>\n",
       "      <td>-0.016017</td>\n",
       "      <td>-0.014040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-06</th>\n",
       "      <td>P9706</td>\n",
       "      <td>0.004966</td>\n",
       "      <td>0.004517</td>\n",
       "      <td>-0.018080</td>\n",
       "      <td>-0.017969</td>\n",
       "      <td>0.003920</td>\n",
       "      <td>0.003710</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-07</th>\n",
       "      <td>P9706</td>\n",
       "      <td>0.007401</td>\n",
       "      <td>0.006454</td>\n",
       "      <td>-0.033309</td>\n",
       "      <td>-0.032130</td>\n",
       "      <td>-0.004250</td>\n",
       "      <td>-0.004629</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-08</th>\n",
       "      <td>P9706</td>\n",
       "      <td>-0.002017</td>\n",
       "      <td>-0.001919</td>\n",
       "      <td>-0.005396</td>\n",
       "      <td>-0.004850</td>\n",
       "      <td>0.008191</td>\n",
       "      <td>0.008967</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           MarkettypeID  RiskPremium1  RiskPremium2      SMB1      SMB2  \\\n",
       "date                                                                      \n",
       "2021-01-04        P9706      0.010357      0.010930  0.004638  0.004161   \n",
       "2021-01-05        P9706      0.010340      0.010769 -0.012563 -0.011595   \n",
       "2021-01-06        P9706      0.004966      0.004517 -0.018080 -0.017969   \n",
       "2021-01-07        P9706      0.007401      0.006454 -0.033309 -0.032130   \n",
       "2021-01-08        P9706     -0.002017     -0.001919 -0.005396 -0.004850   \n",
       "\n",
       "                HML1      HML2  \n",
       "date                            \n",
       "2021-01-04 -0.010758 -0.011928  \n",
       "2021-01-05 -0.016017 -0.014040  \n",
       "2021-01-06  0.003920  0.003710  \n",
       "2021-01-07 -0.004250 -0.004629  \n",
       "2021-01-08  0.008191  0.008967  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2=df[df.MarkettypeID=='P9706']\n",
    "df2.loc[:,['date']] = pd.to_datetime(df2['TradingDate'],format='%Y-%m-%d')\n",
    "df2 = df2.reset_index().set_index('date')\n",
    "df2 = df2.drop(['index','TradingDate'],axis=1)\n",
    "df2=df2.dropna()\n",
    "df3=df2.loc['2021-01-01':'2021-12-01']\n",
    "df3.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ff09b85e-ffce-4a4c-82a7-3352a04e7341",
   "metadata": {},
   "outputs": [],
   "source": [
    "stocks = pd.read_csv('test.csv',encoding=\"utf_8\")\n",
    "stocks['date'] = pd.to_datetime(stocks['date'])\n",
    "stocks = stocks.reset_index().set_index('date')\n",
    "stocks = stocks.drop(['index'],axis=1)\n",
    "returns = np.log(stocks/stocks.shift(1))\n",
    "returns = returns.dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e0ac68c8-d3b3-4259-a38d-b2b19f88ea7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MarkettypeID</th>\n",
       "      <th>RiskPremium1</th>\n",
       "      <th>RiskPremium2</th>\n",
       "      <th>SMB1</th>\n",
       "      <th>SMB2</th>\n",
       "      <th>HML1</th>\n",
       "      <th>HML2</th>\n",
       "      <th>中国重汽</th>\n",
       "      <th>道恩股份</th>\n",
       "      <th>白云机场</th>\n",
       "      <th>东风汽车</th>\n",
       "      <th>中石化</th>\n",
       "      <th>中海油</th>\n",
       "      <th>中石油</th>\n",
       "      <th>上汽</th>\n",
       "      <th>比亚迪</th>\n",
       "      <th>on_b</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-01-05</th>\n",
       "      <td>P9706</td>\n",
       "      <td>0.010340</td>\n",
       "      <td>0.010769</td>\n",
       "      <td>-0.012563</td>\n",
       "      <td>-0.011595</td>\n",
       "      <td>-0.016017</td>\n",
       "      <td>-0.014040</td>\n",
       "      <td>-0.025414</td>\n",
       "      <td>0.061614</td>\n",
       "      <td>-0.000391</td>\n",
       "      <td>-0.016517</td>\n",
       "      <td>-0.037573</td>\n",
       "      <td>-0.002484</td>\n",
       "      <td>-0.036320</td>\n",
       "      <td>-0.041133</td>\n",
       "      <td>-0.002389</td>\n",
       "      <td>-0.209350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-06</th>\n",
       "      <td>P9706</td>\n",
       "      <td>0.004966</td>\n",
       "      <td>0.004517</td>\n",
       "      <td>-0.018080</td>\n",
       "      <td>-0.017969</td>\n",
       "      <td>0.003920</td>\n",
       "      <td>0.003710</td>\n",
       "      <td>0.044676</td>\n",
       "      <td>-0.016507</td>\n",
       "      <td>0.048790</td>\n",
       "      <td>-0.019745</td>\n",
       "      <td>-0.046091</td>\n",
       "      <td>0.022141</td>\n",
       "      <td>0.011384</td>\n",
       "      <td>0.077767</td>\n",
       "      <td>0.016608</td>\n",
       "      <td>-0.179586</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-07</th>\n",
       "      <td>P9706</td>\n",
       "      <td>0.007401</td>\n",
       "      <td>0.006454</td>\n",
       "      <td>-0.033309</td>\n",
       "      <td>-0.032130</td>\n",
       "      <td>-0.004250</td>\n",
       "      <td>-0.004629</td>\n",
       "      <td>0.043059</td>\n",
       "      <td>0.039612</td>\n",
       "      <td>-0.044892</td>\n",
       "      <td>-0.053862</td>\n",
       "      <td>-0.005914</td>\n",
       "      <td>0.002430</td>\n",
       "      <td>0.011256</td>\n",
       "      <td>0.038806</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.410915</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-08</th>\n",
       "      <td>P9706</td>\n",
       "      <td>-0.002017</td>\n",
       "      <td>-0.001919</td>\n",
       "      <td>-0.005396</td>\n",
       "      <td>-0.004850</td>\n",
       "      <td>0.008191</td>\n",
       "      <td>0.008967</td>\n",
       "      <td>-0.005023</td>\n",
       "      <td>0.010916</td>\n",
       "      <td>0.014294</td>\n",
       "      <td>0.026914</td>\n",
       "      <td>-0.027663</td>\n",
       "      <td>0.016848</td>\n",
       "      <td>0.095193</td>\n",
       "      <td>0.018513</td>\n",
       "      <td>0.009368</td>\n",
       "      <td>0.083382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-11</th>\n",
       "      <td>P9706</td>\n",
       "      <td>-0.011561</td>\n",
       "      <td>-0.011943</td>\n",
       "      <td>-0.015109</td>\n",
       "      <td>-0.013678</td>\n",
       "      <td>0.000027</td>\n",
       "      <td>0.001391</td>\n",
       "      <td>0.028327</td>\n",
       "      <td>0.031795</td>\n",
       "      <td>0.051581</td>\n",
       "      <td>-0.023025</td>\n",
       "      <td>-0.030962</td>\n",
       "      <td>-0.031518</td>\n",
       "      <td>0.033862</td>\n",
       "      <td>-0.015058</td>\n",
       "      <td>-0.016452</td>\n",
       "      <td>0.285179</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           MarkettypeID  RiskPremium1  RiskPremium2      SMB1      SMB2  \\\n",
       "date                                                                      \n",
       "2021-01-05        P9706      0.010340      0.010769 -0.012563 -0.011595   \n",
       "2021-01-06        P9706      0.004966      0.004517 -0.018080 -0.017969   \n",
       "2021-01-07        P9706      0.007401      0.006454 -0.033309 -0.032130   \n",
       "2021-01-08        P9706     -0.002017     -0.001919 -0.005396 -0.004850   \n",
       "2021-01-11        P9706     -0.011561     -0.011943 -0.015109 -0.013678   \n",
       "\n",
       "                HML1      HML2      中国重汽      道恩股份      白云机场      东风汽车  \\\n",
       "date                                                                     \n",
       "2021-01-05 -0.016017 -0.014040 -0.025414  0.061614 -0.000391 -0.016517   \n",
       "2021-01-06  0.003920  0.003710  0.044676 -0.016507  0.048790 -0.019745   \n",
       "2021-01-07 -0.004250 -0.004629  0.043059  0.039612 -0.044892 -0.053862   \n",
       "2021-01-08  0.008191  0.008967 -0.005023  0.010916  0.014294  0.026914   \n",
       "2021-01-11  0.000027  0.001391  0.028327  0.031795  0.051581 -0.023025   \n",
       "\n",
       "                 中石化       中海油       中石油        上汽       比亚迪      on_b  \n",
       "date                                                                    \n",
       "2021-01-05 -0.037573 -0.002484 -0.036320 -0.041133 -0.002389 -0.209350  \n",
       "2021-01-06 -0.046091  0.022141  0.011384  0.077767  0.016608 -0.179586  \n",
       "2021-01-07 -0.005914  0.002430  0.011256  0.038806  0.000000  0.410915  \n",
       "2021-01-08 -0.027663  0.016848  0.095193  0.018513  0.009368  0.083382  \n",
       "2021-01-11 -0.030962 -0.031518  0.033862 -0.015058 -0.016452  0.285179  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = pd.concat([df3,returns],axis=1,join='inner')\n",
    "result.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c417e615-51bd-4b24-9fba-0cc449d7baf3",
   "metadata": {},
   "source": [
    "# Fama-Macbeth Regression"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbc76a4e-1189-44e0-9112-87752fecb299",
   "metadata": {},
   "source": [
    "FM method to jointly estimate the market price of factor risk A and the amount of factor risk exposure $\\beta$\n",
    "$$R-r_f=\\alpha_i+\\sum_i \\beta_i\\lambda_i+e_i$$\n",
    "Stage I: time-series to find risk exposure $\\hat{\\beta}_i$\n",
    "\n",
    "Stage II: panel-regression to find risk premium $\\hat{\\lambda}_i$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "730adbea-42bd-4703-9a21-43cbac073566",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Split using both named colums and ix for larger blocks\n",
    "factors = result[[ 'SMB1', 'HML1']].values\n",
    "riskfree =result['RiskPremium1'].values\n",
    "portfolios = result.iloc[:, 8:].values\n",
    "\n",
    "# Use mat for easier linear algebra\n",
    "factors = np.mat(factors)\n",
    "riskfree = np.mat(riskfree)\n",
    "portfolios = np.mat(portfolios)\n",
    "\n",
    "# Shape information\n",
    "T,K = factors.shape\n",
    "T,N = portfolios.shape\n",
    "# Reshape rf and compute excess returns\n",
    "riskfree.shape = T,1\n",
    "excessReturns = portfolios - riskfree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 246,
   "id": "b2af62ce-8f8e-4313-a622-120d3fc6fbc5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>道恩股份</th>\n",
       "      <th>白云机场</th>\n",
       "      <th>东风汽车</th>\n",
       "      <th>中石化</th>\n",
       "      <th>中海油</th>\n",
       "      <th>中石油</th>\n",
       "      <th>上汽</th>\n",
       "      <th>比亚迪</th>\n",
       "      <th>on_b</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.877937</td>\n",
       "      <td>0.576227</td>\n",
       "      <td>0.451469</td>\n",
       "      <td>1.535050</td>\n",
       "      <td>-0.038104</td>\n",
       "      <td>0.042708</td>\n",
       "      <td>-1.131218</td>\n",
       "      <td>0.083111</td>\n",
       "      <td>-1.960756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.869552</td>\n",
       "      <td>0.573020</td>\n",
       "      <td>0.446882</td>\n",
       "      <td>1.499976</td>\n",
       "      <td>-0.026563</td>\n",
       "      <td>0.019524</td>\n",
       "      <td>-1.163655</td>\n",
       "      <td>0.084560</td>\n",
       "      <td>-2.073204</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.949749</td>\n",
       "      <td>0.758461</td>\n",
       "      <td>0.390940</td>\n",
       "      <td>1.448385</td>\n",
       "      <td>0.002383</td>\n",
       "      <td>0.030693</td>\n",
       "      <td>-1.004208</td>\n",
       "      <td>0.112255</td>\n",
       "      <td>-2.873434</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.976178</td>\n",
       "      <td>0.588962</td>\n",
       "      <td>0.130269</td>\n",
       "      <td>1.670652</td>\n",
       "      <td>0.028177</td>\n",
       "      <td>0.076217</td>\n",
       "      <td>-0.964546</td>\n",
       "      <td>0.132237</td>\n",
       "      <td>-0.843163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.938442</td>\n",
       "      <td>0.652441</td>\n",
       "      <td>0.179566</td>\n",
       "      <td>1.659397</td>\n",
       "      <td>0.044440</td>\n",
       "      <td>0.267439</td>\n",
       "      <td>-0.937347</td>\n",
       "      <td>0.138115</td>\n",
       "      <td>-0.747200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       道恩股份      白云机场      东风汽车       中石化       中海油       中石油        上汽  \\\n",
       "0 -0.877937  0.576227  0.451469  1.535050 -0.038104  0.042708 -1.131218   \n",
       "1 -0.869552  0.573020  0.446882  1.499976 -0.026563  0.019524 -1.163655   \n",
       "2 -0.949749  0.758461  0.390940  1.448385  0.002383  0.030693 -1.004208   \n",
       "3 -0.976178  0.588962  0.130269  1.670652  0.028177  0.076217 -0.964546   \n",
       "4 -0.938442  0.652441  0.179566  1.659397  0.044440  0.267439 -0.937347   \n",
       "\n",
       "        比亚迪      on_b  \n",
       "0  0.083111 -1.960756  \n",
       "1  0.084560 -2.073204  \n",
       "2  0.112255 -2.873434  \n",
       "3  0.132237 -0.843163  \n",
       "4  0.138115 -0.747200  "
      ]
     },
     "execution_count": 246,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算beta\n",
    "column = result.iloc[:, 8:].columns.tolist()\n",
    "beta1 = pd.DataFrame()\n",
    "beta2 = pd.DataFrame()\n",
    "x = sm.add_constant(factors)\n",
    "for i in range(portfolios.shape[1]):\n",
    "    rolstime1 = RollingOLS(excessReturns[:,i], x, window=60)\n",
    "    rres = rolstime1.fit()\n",
    "    beta1[i]=rres.params.copy()[:,1]\n",
    "    beta2[i]=rres.params.copy()[:,2]\n",
    "beta1 = beta1.dropna().reset_index(drop=True)\n",
    "beta2 = beta2.dropna().reset_index(drop=True)\n",
    "beta1.columns = column\n",
    "beta2.columns = column\n",
    "beta1.head() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 249,
   "id": "222c1983-1e1d-45b0-984b-2483ce8bbb1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>道恩股份</th>\n",
       "      <th>白云机场</th>\n",
       "      <th>东风汽车</th>\n",
       "      <th>中石化</th>\n",
       "      <th>中海油</th>\n",
       "      <th>中石油</th>\n",
       "      <th>上汽</th>\n",
       "      <th>比亚迪</th>\n",
       "      <th>on_b</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.877937</td>\n",
       "      <td>0.576227</td>\n",
       "      <td>0.451469</td>\n",
       "      <td>1.535050</td>\n",
       "      <td>-0.038104</td>\n",
       "      <td>0.042708</td>\n",
       "      <td>-1.131218</td>\n",
       "      <td>0.083111</td>\n",
       "      <td>-1.960756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.869552</td>\n",
       "      <td>0.573020</td>\n",
       "      <td>0.446882</td>\n",
       "      <td>1.499976</td>\n",
       "      <td>-0.026563</td>\n",
       "      <td>0.019524</td>\n",
       "      <td>-1.163655</td>\n",
       "      <td>0.084560</td>\n",
       "      <td>-2.073204</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.949749</td>\n",
       "      <td>0.758461</td>\n",
       "      <td>0.390940</td>\n",
       "      <td>1.448385</td>\n",
       "      <td>0.002383</td>\n",
       "      <td>0.030693</td>\n",
       "      <td>-1.004208</td>\n",
       "      <td>0.112255</td>\n",
       "      <td>-2.873434</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.976178</td>\n",
       "      <td>0.588962</td>\n",
       "      <td>0.130269</td>\n",
       "      <td>1.670652</td>\n",
       "      <td>0.028177</td>\n",
       "      <td>0.076217</td>\n",
       "      <td>-0.964546</td>\n",
       "      <td>0.132237</td>\n",
       "      <td>-0.843163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.938442</td>\n",
       "      <td>0.652441</td>\n",
       "      <td>0.179566</td>\n",
       "      <td>1.659397</td>\n",
       "      <td>0.044440</td>\n",
       "      <td>0.267439</td>\n",
       "      <td>-0.937347</td>\n",
       "      <td>0.138115</td>\n",
       "      <td>-0.747200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       道恩股份      白云机场      东风汽车       中石化       中海油       中石油        上汽  \\\n",
       "0 -0.877937  0.576227  0.451469  1.535050 -0.038104  0.042708 -1.131218   \n",
       "1 -0.869552  0.573020  0.446882  1.499976 -0.026563  0.019524 -1.163655   \n",
       "2 -0.949749  0.758461  0.390940  1.448385  0.002383  0.030693 -1.004208   \n",
       "3 -0.976178  0.588962  0.130269  1.670652  0.028177  0.076217 -0.964546   \n",
       "4 -0.938442  0.652441  0.179566  1.659397  0.044440  0.267439 -0.937347   \n",
       "\n",
       "        比亚迪      on_b  \n",
       "0  0.083111 -1.960756  \n",
       "1  0.084560 -2.073204  \n",
       "2  0.112255 -2.873434  \n",
       "3  0.132237 -0.843163  \n",
       "4  0.138115 -0.747200  "
      ]
     },
     "execution_count": 249,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算lambda\n",
    "lambda1 = pd.DataFrame()\n",
    "lambda2 = pd.DataFrame()\n",
    "x = sm.add_constant(factors)\n",
    "\n",
    "for i in range(portfolios.shape[1]):\n",
    "    rolstime1 = RollingOLS(excessReturns[:,i], x, window=60)\n",
    "    rres = rolstime1.fit()\n",
    "    lambda1[i]=rres.params.copy()[:,1]\n",
    "    lambda1[i]=rres.params.copy()[:,2]\n",
    "\n",
    "beta1 = beta1.dropna().reset_index(drop=True)\n",
    "beta2 = beta2.dropna().reset_index(drop=True)\n",
    "beta1.columns = column\n",
    "beta2.columns = column\n",
    "beta1.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 227,
   "id": "c630c362-45f1-4e07-9ecb-0f0953912052",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>道恩股份</th>\n",
       "      <th>白云机场</th>\n",
       "      <th>东风汽车</th>\n",
       "      <th>中石化</th>\n",
       "      <th>中海油</th>\n",
       "      <th>中石油</th>\n",
       "      <th>上汽</th>\n",
       "      <th>比亚迪</th>\n",
       "      <th>on_b</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-01-05</th>\n",
       "      <td>-0.002590</td>\n",
       "      <td>-0.003509</td>\n",
       "      <td>-0.000929</td>\n",
       "      <td>-0.005740</td>\n",
       "      <td>0.001128</td>\n",
       "      <td>-0.002579</td>\n",
       "      <td>0.001641</td>\n",
       "      <td>0.000323</td>\n",
       "      <td>0.011074</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-06</th>\n",
       "      <td>-0.003897</td>\n",
       "      <td>-0.003350</td>\n",
       "      <td>-0.000331</td>\n",
       "      <td>-0.005110</td>\n",
       "      <td>0.001649</td>\n",
       "      <td>-0.001730</td>\n",
       "      <td>0.002514</td>\n",
       "      <td>0.000651</td>\n",
       "      <td>0.015522</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-07</th>\n",
       "      <td>-0.003590</td>\n",
       "      <td>-0.004025</td>\n",
       "      <td>-0.000262</td>\n",
       "      <td>-0.004522</td>\n",
       "      <td>0.001362</td>\n",
       "      <td>-0.001657</td>\n",
       "      <td>0.001162</td>\n",
       "      <td>0.000417</td>\n",
       "      <td>0.017848</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-08</th>\n",
       "      <td>-0.004351</td>\n",
       "      <td>-0.003299</td>\n",
       "      <td>0.000906</td>\n",
       "      <td>-0.004259</td>\n",
       "      <td>0.001706</td>\n",
       "      <td>-0.001822</td>\n",
       "      <td>0.000775</td>\n",
       "      <td>0.000726</td>\n",
       "      <td>0.011648</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-11</th>\n",
       "      <td>-0.004699</td>\n",
       "      <td>-0.003942</td>\n",
       "      <td>0.000441</td>\n",
       "      <td>-0.003956</td>\n",
       "      <td>0.001589</td>\n",
       "      <td>-0.003245</td>\n",
       "      <td>0.000165</td>\n",
       "      <td>0.000773</td>\n",
       "      <td>0.010515</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                道恩股份      白云机场      东风汽车       中石化       中海油       中石油  \\\n",
       "date                                                                     \n",
       "2021-01-05 -0.002590 -0.003509 -0.000929 -0.005740  0.001128 -0.002579   \n",
       "2021-01-06 -0.003897 -0.003350 -0.000331 -0.005110  0.001649 -0.001730   \n",
       "2021-01-07 -0.003590 -0.004025 -0.000262 -0.004522  0.001362 -0.001657   \n",
       "2021-01-08 -0.004351 -0.003299  0.000906 -0.004259  0.001706 -0.001822   \n",
       "2021-01-11 -0.004699 -0.003942  0.000441 -0.003956  0.001589 -0.003245   \n",
       "\n",
       "                  上汽       比亚迪      on_b  \n",
       "date                                      \n",
       "2021-01-05  0.001641  0.000323  0.011074  \n",
       "2021-01-06  0.002514  0.000651  0.015522  \n",
       "2021-01-07  0.001162  0.000417  0.017848  \n",
       "2021-01-08  0.000775  0.000726  0.011648  \n",
       "2021-01-11  0.000165  0.000773  0.010515  "
      ]
     },
     "execution_count": 227,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算超额收益率\n",
    "portfolios1 = result.iloc[:, 8:]\n",
    "excessReturns1 = portfolios1.sub(result['RiskPremium1'],axis = 0)\n",
    "excessReturns2 = excessReturns1\n",
    "for i in range(59):\n",
    "    excessReturns2=excessReturns2+excessReturns1.shift(-i-1)\n",
    "excessReturns2=excessReturns2.dropna()/60\n",
    "excessReturns2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 260,
   "id": "bd28334a-e54f-4eda-9992-0b8611959536",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.0022854,  0.001938 ])"
      ]
     },
     "execution_count": 260,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#lambda\n",
    "lambdas = np.zeros(shape=(excessReturns2.shape[0],2))\n",
    "for i in range(excessReturns2.shape[0]):\n",
    "    x = DataFrame([beta1.iloc[i],beta2.iloc[i]])\n",
    "    y=excessReturns2.iloc[i,:]\n",
    "    model = sm.OLS(y.T,x.T)\n",
    "    lambdas[i] = results.params\n",
    "np.mean(lambdas,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf131ca6-3398-4dc1-a950-f027cd323243",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
